import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * ldap.js - LDAP model
 * @date: 2018-12-20
 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { queryIdpValue, getPublicKey as _getPublicKey } from 'cpaas-front/lib/services/api';
import { fetchLDAP as _fetchLDAP, updateLDAP as _updateLDAP, testConnect as _testConnect, disabledLDAP as _disabledLDAP, enabledLDAP as _enabledLDAP, fetchSyncInfo as _fetchSyncInfo, fetchSyncErrorInfo as _fetchSyncErrorInfo, syncUser as _syncUser, stopSyncUser as _stopSyncUser, querySyncLeave as _querySyncLeave, querySyncUser as _querySyncUser, updateSyncLeave as _updateSyncLeave, updateSyncUser as _updateSyncUser } from "../services/ldapService";
export default {
  namespace: 'ldap',
  state: {
    ldapData: {},
    // LDAP数据
    testData: {},
    syncInfo: {},
    syncPagination: {},
    syncErrorInfo: {},
    syncErrorPagination: {},
    directoryTypeList: [],
    // 目录类型
    publicKey: '',
    // 密码公钥
    frequencyList: [],
    syncUserDetail: {},
    syncLeaveDetail: {}
  },
  effects: {
    // 获取目录类信息
    queryDirectoryType: /*#__PURE__*/_regeneratorRuntime.mark(function queryDirectoryType(_, _ref) {
      var call, put, res, directoryTypeList;
      return _regeneratorRuntime.wrap(function queryDirectoryType$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryIdpValue, 'CIAM.LDAP.DIR_TYPE');

            case 3:
              res = _context.sent;
              directoryTypeList = getResponse(res);
              _context.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  directoryTypeList: directoryTypeList
                }
              });

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, queryDirectoryType);
    }),
    queryFrequency: /*#__PURE__*/_regeneratorRuntime.mark(function queryFrequency(_, _ref2) {
      var call, put, res, frequencyList;
      return _regeneratorRuntime.wrap(function queryFrequency$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref2.call, put = _ref2.put;
              _context2.next = 3;
              return call(queryIdpValue, 'CIAM.LDAP_SYNC_FREQUENCY');

            case 3:
              res = _context2.sent;
              frequencyList = getResponse(res);
              _context2.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  frequencyList: frequencyList
                }
              });

            case 7:
            case "end":
              return _context2.stop();
          }
        }
      }, queryFrequency);
    }),
    // 查询LDAP数据
    fetchLDAP: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLDAP(_ref3, _ref4) {
      var payload, call, put, res, ldapData;
      return _regeneratorRuntime.wrap(function fetchLDAP$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context3.next = 4;
              return call(_fetchLDAP, payload);

            case 4:
              res = _context3.sent;
              ldapData = getResponse(res);

              if (!ldapData) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  ldapData: ldapData
                }
              });

            case 9:
              return _context3.abrupt("return", ldapData);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchLDAP);
    }),
    // 更新LDAP
    updateLDAP: /*#__PURE__*/_regeneratorRuntime.mark(function updateLDAP(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateLDAP$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context4.next = 4;
              return call(_updateLDAP, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateLDAP);
    }),
    // 连接测试
    testConnect: /*#__PURE__*/_regeneratorRuntime.mark(function testConnect(_ref7, _ref8) {
      var payload, call, put, res, testData;
      return _regeneratorRuntime.wrap(function testConnect$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context5.next = 4;
              return call(_testConnect, payload);

            case 4:
              res = _context5.sent;
              testData = getResponse(res);

              if (!testData) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  testData: testData
                }
              });

            case 9:
              return _context5.abrupt("return", testData);

            case 10:
            case "end":
              return _context5.stop();
          }
        }
      }, testConnect);
    }),
    // 禁用LDAP
    disabledLDAP: /*#__PURE__*/_regeneratorRuntime.mark(function disabledLDAP(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function disabledLDAP$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context6.next = 4;
              return call(_disabledLDAP, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, disabledLDAP);
    }),
    // 启用LDAP
    enabledLDAP: /*#__PURE__*/_regeneratorRuntime.mark(function enabledLDAP(_ref11, _ref12) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function enabledLDAP$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context7.next = 4;
              return call(_enabledLDAP, payload);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, enabledLDAP);
    }),
    // 查询数据同步信息
    fetchSyncInfo: /*#__PURE__*/_regeneratorRuntime.mark(function fetchSyncInfo(_ref13, _ref14) {
      var payload, call, put, res, syncInfo;
      return _regeneratorRuntime.wrap(function fetchSyncInfo$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call, put = _ref14.put;
              _context8.next = 4;
              return call(_fetchSyncInfo, parseParameters(payload));

            case 4:
              res = _context8.sent;
              syncInfo = getResponse(res);

              if (!syncInfo) {
                _context8.next = 9;
                break;
              }

              _context8.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  syncInfo: syncInfo,
                  syncPagination: createPagination(syncInfo)
                }
              });

            case 9:
              return _context8.abrupt("return", syncInfo);

            case 10:
            case "end":
              return _context8.stop();
          }
        }
      }, fetchSyncInfo);
    }),
    // 查询数据同步信息
    fetchSyncErrorInfo: /*#__PURE__*/_regeneratorRuntime.mark(function fetchSyncErrorInfo(_ref15, _ref16) {
      var payload, call, put, res, syncErrorInfo;
      return _regeneratorRuntime.wrap(function fetchSyncErrorInfo$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call, put = _ref16.put;
              _context9.next = 4;
              return call(_fetchSyncErrorInfo, parseParameters(payload));

            case 4:
              res = _context9.sent;
              syncErrorInfo = getResponse(res);

              if (!syncErrorInfo) {
                _context9.next = 9;
                break;
              }

              _context9.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  syncErrorInfo: syncErrorInfo,
                  syncErrorPagination: createPagination(syncErrorInfo)
                }
              });

            case 9:
              return _context9.abrupt("return", syncErrorInfo);

            case 10:
            case "end":
              return _context9.stop();
          }
        }
      }, fetchSyncErrorInfo);
    }),
    // 同步用户
    syncUser: /*#__PURE__*/_regeneratorRuntime.mark(function syncUser(_ref17, _ref18) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function syncUser$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context10.next = 4;
              return call(_syncUser, payload);

            case 4:
              res = _context10.sent;
              return _context10.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, syncUser);
    }),
    // 终止同步用户
    stopSyncUser: /*#__PURE__*/_regeneratorRuntime.mark(function stopSyncUser(_ref19, _ref20) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function stopSyncUser$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              _context11.next = 4;
              return call(_stopSyncUser, payload);

            case 4:
              res = _context11.sent;
              return _context11.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, stopSyncUser);
    }),
    // 请求公钥
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref21) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              call = _ref21.call, put = _ref21.put;
              _context12.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context12.sent;

              if (!res) {
                _context12.next = 7;
                break;
              }

              _context12.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context12.abrupt("return", res);

            case 8:
            case "end":
              return _context12.stop();
          }
        }
      }, getPublicKey);
    }),
    // 查询LDAP数据
    querySyncLeave: /*#__PURE__*/_regeneratorRuntime.mark(function querySyncLeave(_ref22, _ref23) {
      var payload, call, put, res, syncLeaveDetail;
      return _regeneratorRuntime.wrap(function querySyncLeave$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref22.payload;
              call = _ref23.call, put = _ref23.put;
              _context13.next = 4;
              return call(_querySyncLeave, payload);

            case 4:
              res = _context13.sent;
              syncLeaveDetail = getResponse(res);

              if (!syncLeaveDetail) {
                _context13.next = 9;
                break;
              }

              _context13.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  syncLeaveDetail: syncLeaveDetail
                }
              });

            case 9:
              return _context13.abrupt("return", syncLeaveDetail);

            case 10:
            case "end":
              return _context13.stop();
          }
        }
      }, querySyncLeave);
    }),
    querySyncUser: /*#__PURE__*/_regeneratorRuntime.mark(function querySyncUser(_ref24, _ref25) {
      var payload, call, put, res, syncUserDetail;
      return _regeneratorRuntime.wrap(function querySyncUser$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              payload = _ref24.payload;
              call = _ref25.call, put = _ref25.put;
              _context14.next = 4;
              return call(_querySyncUser, payload);

            case 4:
              res = _context14.sent;
              syncUserDetail = getResponse(res);

              if (!_syncUser) {
                _context14.next = 9;
                break;
              }

              _context14.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  syncUserDetail: syncUserDetail
                }
              });

            case 9:
              return _context14.abrupt("return", syncUserDetail);

            case 10:
            case "end":
              return _context14.stop();
          }
        }
      }, querySyncUser);
    }),
    updateSyncLeave: /*#__PURE__*/_regeneratorRuntime.mark(function updateSyncLeave(_ref26, _ref27) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateSyncLeave$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              payload = _ref26.payload;
              call = _ref27.call;
              _context15.next = 4;
              return call(_updateSyncLeave, payload);

            case 4:
              res = _context15.sent;
              return _context15.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context15.stop();
          }
        }
      }, updateSyncLeave);
    }),
    updateSyncUser: /*#__PURE__*/_regeneratorRuntime.mark(function updateSyncUser(_ref28, _ref29) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateSyncUser$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              payload = _ref28.payload;
              call = _ref29.call;
              _context16.next = 4;
              return call(_updateSyncUser, payload);

            case 4:
              res = _context16.sent;
              return _context16.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context16.stop();
          }
        }
      }, updateSyncUser);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};